package com.blue.jdbc.config;

import com.blue.jdbc.dialect.MySQLDialect;
import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

/**
 * DataSource解析
 *
 * @author Jin Zheng
 * @date 2018-10-31
 */
public class DataSourceParser extends AbstractSimpleBeanDefinitionParser
{
	public DataSourceParser()
	{
	}

	@Override
	protected Class<?> getBeanClass(Element element)
	{
		return BasicDataSource.class;
	}

	@Override
	protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder)
	{
		try
		{
			builder.addPropertyValue("url", element.getAttribute("url"));
			builder.addPropertyValue("username", element.getAttribute("username"));
			builder.addPropertyValue("password", element.getAttribute("password"));
			builder.addPropertyValue("driverClassName", element.getAttribute("driver-class-name"));
			builder.addPropertyValue("validationQuery", element.getAttribute("validation-query"));
			builder.addPropertyValue("initialSize", element.getAttribute("initial-size"));
			builder.addPropertyValue("maxTotal", element.getAttribute("max-total"));
			builder.addPropertyValue("maxIdle", element.getAttribute("max-idle"));
			builder.addPropertyValue("minIdle", element.getAttribute("min-idle"));
			builder.addPropertyValue("maxWaitMillis", element.getAttribute("max-wait-millis"));
		}
		catch (Exception e)
		{
			parserContext.getReaderContext().error(MySQLDialect.class.getName() + " 无法创建", element, null, e);
		}
	}
}
